#include "template.h"

#include "util/fs.h"
#include "util/collector/for.h"

#include "builder.h"


namespace html::tmplt
{

Template::Template(const fs::path &path) {
    Builder builder(path);
    builder(parts_, deps_);
}

void Template::render(const std::map<std::string, DynamicObj> &objs, std::ostream &out) const {
    for (const Part &part : parts_) {
        if (part.isText()) {
            out << part.content();
            continue;
        }
        const auto it = objs.find(part.content());
        if (it == objs.end()) {
            out << "{" << part.content() << "}";
            continue;
        }
        out << it->second;
    }
}

bool Template::isOutdated(fs::file_time_type createTime) const {
    for (const fs::path &dep : deps_) {
        if (fs::last_write_time(dep) > createTime) {
            return true;
        }
    }
    return false;
}

} // namespace html::tmplt
